package cn.iocoder.yudao.module.warehouseUsage.controller.admin.warehouseUsage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.warehouseUsage.controller.admin.warehouseUsage.vo.*;
import cn.iocoder.yudao.module.warehouseUsage.dal.dataobject.warehouseUsage.warehouseUsageDO;
import cn.iocoder.yudao.module.warehouseUsage.service.warehouseUsage.warehouseUsageService;

@Tag(name = "管理后台 - 使用记录")
@RestController
@RequestMapping("/warehouseUsage/warehouse-usage")
@Validated
public class warehouseUsageController {

    @Resource
    private warehouseUsageService warehouseUsageService;

    @PostMapping("/create")
    @Operation(summary = "创建使用记录")
    @PreAuthorize("@ss.hasPermission('warehouseUsage:warehouse-usage:create')")
    public CommonResult<Long> createwarehouseUsage(@Valid @RequestBody warehouseUsageSaveReqVO createReqVO) {
        return success(warehouseUsageService.createwarehouseUsage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新使用记录")
    @PreAuthorize("@ss.hasPermission('warehouseUsage:warehouse-usage:update')")
    public CommonResult<Boolean> updatewarehouseUsage(@Valid @RequestBody warehouseUsageSaveReqVO updateReqVO) {
        warehouseUsageService.updatewarehouseUsage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除使用记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('warehouseUsage:warehouse-usage:delete')")
    public CommonResult<Boolean> deletewarehouseUsage(@RequestParam("id") Long id) {
        warehouseUsageService.deletewarehouseUsage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除使用记录")
                @PreAuthorize("@ss.hasPermission('warehouseUsage:warehouse-usage:delete')")
    public CommonResult<Boolean> deletewarehouseUsageList(@RequestParam("ids") List<Long> ids) {
        warehouseUsageService.deletewarehouseUsageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得使用记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('warehouseUsage:warehouse-usage:query')")
    public CommonResult<warehouseUsageRespVO> getwarehouseUsage(@RequestParam("id") Long id) {
        warehouseUsageDO warehouseUsage = warehouseUsageService.getwarehouseUsage(id);
        return success(BeanUtils.toBean(warehouseUsage, warehouseUsageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得使用记录分页")
    @PreAuthorize("@ss.hasPermission('warehouseUsage:warehouse-usage:query')")
    public CommonResult<PageResult<warehouseUsageRespVO>> getwarehouseUsagePage(@Valid warehouseUsagePageReqVO pageReqVO) {
        PageResult<warehouseUsageDO> pageResult = warehouseUsageService.getwarehouseUsagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, warehouseUsageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出使用记录 Excel")
    @PreAuthorize("@ss.hasPermission('warehouseUsage:warehouse-usage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportwarehouseUsageExcel(@Valid warehouseUsagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<warehouseUsageDO> list = warehouseUsageService.getwarehouseUsagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "使用记录.xls", "数据", warehouseUsageRespVO.class,
                        BeanUtils.toBean(list, warehouseUsageRespVO.class));
    }

}